package com.banzhi.shop.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.adapter.OrderManagementAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.OrderManagement.Order_Data;
import com.banzhi.shop.entity.videocollege.VideoOrder_Data;
import com.banzhi.shop.fragment.VIPOrderFragment;
import com.banzhi.shop.model.CallBack.FindVideosCallBack;
import com.banzhi.shop.model.Impl.FindVideosImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.R.id.et_context;
import static com.banzhi.shop.R.id.srlayout;
import static com.banzhi.shop.fragment.GoodsParameterFrg.TAG;
import static com.loopj.android.http.AsyncHttpClient.log;
import static com.mob.MobSDK.getContext;

//视频订单搜索
public class FindOrderActivity2 extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener,FindVideosCallBack {

    private FindVideosImpl findVideosModel;
    /**
     * 退出
     */
    private ImageView ivback;
    /**
     * 列表
     */
    private ListView listView;
    /**
     * 下拉刷新
     */
    private SwipeRefreshLayout srllistview;
    private EditText etfind;
    private RelativeLayout rlnodata;
    /**
     * 判断listview是不是滑到最低端了
     */
    private boolean isbottom;
    private List<VideoOrder_Data> videoOrderData = new ArrayList<VideoOrder_Data>();

    private RecyclerView rvOrderFind;
    private LinearLayoutManager LayoutManager;
    private CommonAdapter<VideoOrder_Data> adapter;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_order2);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        findVideosModel = new FindVideosImpl();
        findVideosModel.getData(etfind.getText().toString().trim(),page,"2",this);
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        srllistview = (SwipeRefreshLayout) findViewById(srlayout);
       // listView = (ListView) findViewById(R.id.listview);

        rvOrderFind = (RecyclerView) findViewById(R.id.rv_order_find);
        LayoutManager = new LinearLayoutManager(this);
        LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvOrderFind .setLayoutManager(LayoutManager);
        srllistview.setColorSchemeResources(R.color.themeColor);
        srllistview.setProgressBackgroundColorSchemeResource(R.color.colorWhite);

        rlnodata = (RelativeLayout) findViewById(R.id.rl_nodata);
        etfind = (EditText) findViewById(R.id.et_find);

    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        srllistview.setOnRefreshListener(this);
        etfind.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                {
                    if (editable.length() > 0) {
                        page = 1;
                        findVideosModel.getData(etfind.getText().toString().trim(),page,"2",FindOrderActivity2.this);;
                    }else {
                        findVideosModel.getData(page,FindOrderActivity2.this);
                    }

                }

            }
        });
        rvOrderFind.addOnScrollListener(new EndlessRecyclerOnScrollListener(LayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                if (etfind.getText().length() == 0) {
                    LoadingDialogUtils.showProgress(FindOrderActivity2.this, "正在加载,请稍后~");
                    findVideosModel.getData(page, FindOrderActivity2.this);
                }else {
                LoadingDialogUtils.showProgress(FindOrderActivity2.this, "正在加载,请稍后~");
                findVideosModel.getData(etfind.getText().toString().trim(),page,"2",FindOrderActivity2.this);
                }
            }
        });
    }


    @Override
    public void onRefresh() {
        page = 1;
        if (etfind.getText().length() == 0) {
            LoadingDialogUtils.showProgress(FindOrderActivity2.this, "正在加载,请稍后~");
            findVideosModel.getData(page,FindOrderActivity2.this);
        } else {
            LoadingDialogUtils.showProgress(FindOrderActivity2.this, "正在加载,请稍后~");
            findVideosModel.getData(etfind.getText().toString().trim(), page, "2", FindOrderActivity2.this);
        if (srllistview.isRefreshing()) {
            srllistview.setRefreshing(false);
        }

        }
        LoadingDialogUtils.dismiss();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }

    }

    @Override
    public void ongetDataSuccess(Object object) {
        final List<VideoOrder_Data> data = (List<VideoOrder_Data>) object;
        if (page == 1) {
            videoOrderData.clear();
        } else {
            if (data.size() == 0) {

                return;
            }
        }
        if (data.size() > 0) {
            videoOrderData.addAll(data);
        }

        if (videoOrderData.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        if (adapter == null) {adapter = new CommonAdapter<VideoOrder_Data>(this, R.layout.item_video_order, videoOrderData) {
            @Override
            public void convert(ViewHolder holder, final VideoOrder_Data data, int position) {
                holder.setText(R.id.tv_ordernum, data.getOrderSn());
                holder.getTextView(R.id.tv_fuzhi).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ShareUtils.copyLianjie(FindOrderActivity2.this, data.getOrderSn());}
                });
                holder.setText(R.id.tv_video_name, data.getGoodsName());
       //       holder.setText(R.id.tv_time_long,data.getPeriod());
                if (ASUtils.isCanUse(data.getCover())) {
                    Picasso.with(getContext()).load(data.getCover()).placeholder(R.mipmap.zhanweitu2).into(holder.getImageView(R.id.iv_image));
                }

                holder.setText(R.id.tv_video_yuan, data.getPrice());
                holder.setText(R.id.tv_teacher, data.getTeacherName());
                holder.setText(R.id.tv_statu_time, data.getPayTime());

            }
        };
            rvOrderFind.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

        if (srllistview.isRefreshing()) {
            srllistview.setRefreshing(false);
        }
        LoadingDialogUtils.dismiss();
    }


    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
      // NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }
}
